export function infos() {
    let index = 0;
    let is_move = true;
    let tabs = [{
            key: 'COMPANY_NEWS',
            value: '公司新闻',
            page: 0,
            row: 4,
            itemBox_index: 1,
            res_len: 0,
        },
        {
            key: 'VIDEO_AREA',
            value: '视频专区',
            page: 0,
            row: 4,
            itemBox_index: 1,
            res_len: 0,
        },
        {
            key: 'INDUSTRY_DYNAMICS',
            value: '行业动态',
            page: 0,
            row: 4,
            itemBox_index: 1,
            res_len: 0,
        },
        {
            key: 'RESEARCH_REPORT',
            value: '行研报告',
            page: 0,
            row: 4,
            itemBox_index: 1,
            res_len: 0,
        }
    ];

    $(".infos .tab").on("click", function (e) {
        let target = e.target;
        $(target).addClass("active").siblings(".tab").removeClass("active");
        let i = $(target).attr("data-index");
        $(`.infos .tabContent${i}`).addClass("active").siblings(".tabContent").removeClass("active");
        index = i - 1;
    });



    function loadData(index, cb) {
        $(".loading").css("display", "block");
        let obj = {
            type: tabs[index].key,
            page: tabs[index].page,
            row: tabs[index].row
        };
        $.ajax({
            url: "/c/News/lists",
            type: "POST",
            data: obj,
            success: function (res) {
                showData(res, index);
                if (cb && typeof cb == "function") {
                    cb(index);
                }
            }
        });
    }

    function showData(res, index) {
        is_move = false;
        let html = "";
        let itemHtml = "";
        if (res) {
            let len = res.list.length;
            tabs[index].res_len = len;
            for (let i = 0; i < len; i++) {
                let item = res.list[i];
                let content = $(`<div>${item.content}</div>`).text();
                html += `
                 <a href="/article.html?id=${item.id}" class="item" target="_blank">
                    <div class="imgBox">
                        <img src="${item.cover}"/>
                    </div>
                    <div class="contentBox">
                        <div>
                            <div class="title">
                                ${item.title}
                            </div>
                            <div class="intro">
                                ${content}
                            </div>
                        </div>
                        <div class="time">${item.pubTime}</div>
                    </div>
                </a>
                `;
                if ((i + 1) % 4 == 0) {
                    itemHtml = `<div class='itemBox' data-index="${tabs[index].itemBox_index}">${html}</div>`;
                    $(`.tabContent${index+1}`).append(itemHtml);
                    html = "";
                    tabs[index].itemBox_index++;
                } else {
                    if (len % 4 !== 0) {
                        if (len / 4 !== 1) {
                            if (len - (i + 1) < 4) {
                                if (i + 1 == len) {
                                    itemHtml = `<div class='itemBox' data-index="${tabs[index].itemBox_index}">${html}</div>`;
                                    $(`.tabContent${index+1}`).append(itemHtml);
                                    tabs[index].itemBox_index++;
                                }
                            }
                        }
                    }
                }
            }
            if (len > 0) {
                tabs[index].page++;
            }
            is_move = true;
            $(".loading").css("display", "none");
        }
    }

    loadData(0, loadData);
    loadData(1, loadData);
    loadData(2, loadData);
    loadData(3, loadData);



    let position = 0;
    let bodyWidth = $("body").width();

    function left_right_move(direction) {
        position = position + direction * bodyWidth;
        $(`.tabContent${index+1}`).css("transform", `translateX(${position}px)`);
    }


    function right_move() {
        if (position == 0) {
            return;
        }
        if (is_move) {
            is_move = false;
            left_right_move(1);
        }
    }

    function left_move() {
        if (Math.abs(position) == ($(`.tabContent${index+1} .itemBox`).length - 1) * bodyWidth) {
            return;
        }
        if (is_move) {
            if (tabs[index].res_len != 0) {
                let active_index = Math.abs(position / bodyWidth) + 1;
                let loaded_len = $(`.tabContent${index+1} .itemBox`).length;
                if (loaded_len - active_index < 2) {
                    loadData(index);
                } else {
                    is_move = false;
                }
            } else {
                is_move = false;
            }
            left_right_move(-1);
        }
    }


    $(".left_move").on("click", function () {
        right_move();
        is_move = true;
    });
    $(".right_move").on("click", function () {
        left_move();
        is_move = true;
    });
}